﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace logic
{
    public class DateTimeHelper
    {
        private static int HanyManyWeeksInAYear(int year)
        {
            System.DateTime fDt = DateTime.Parse(year + "-01-01");
            int k = Convert.ToInt32(fDt.DayOfWeek);
            if (k == 1)
            {
                int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
                int countWeek = countDay / 7 + 1;
                return countWeek;

            }
            else
            {
                int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
                int countWeek = countDay / 7 + 2;
                return countWeek;
            } 

        }

        public static void GetWeek(int nYear, int nNumWeek, out    DateTime dtWeekStart, out    DateTime dtWeekeEnd)
        {
            DateTime dt = new DateTime(nYear, 1, 1);
            dt = dt + new TimeSpan((nNumWeek - 1) * 7, 0, 0, 0);
            dtWeekStart = dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday);
            dtWeekeEnd = dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1);
        }


        public static List<WeekEntity> GetWeeks(int year)
        {
            List<WeekEntity> weeks = new List<WeekEntity>();

            DateTime startDate = DateTime.MinValue;
            DateTime endDate = DateTime.MinValue;

            WeekEntity weekEntity = null;

            for (int i = 1; i <= 53; i++)
            {
                GetWeek(year, i, out  startDate, out  endDate);
                weekEntity = new WeekEntity();
                weekEntity.Week = i;
                weekEntity.StartDate = startDate;
                weekEntity.EndDate = endDate;

                weeks.Add(weekEntity);
            }

            return weeks;
        }
    }
}
